package com.tanhua.mangement.service;

import cn.hutool.core.convert.Convert;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Movements;
import com.tanhua.dubbo.server.pojo.Publish;

import com.tanhua.mangement.enums.StateEnum;
import com.tanhua.mangement.vo.PageResult;
import com.tanhua.mangement.vo.Totals;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Service
public class MessagesManageService {
    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    @Autowired
    UserInfoMapper userInfoMapper;



    /**
     *
     * @param pagesize  条数
     * @param page  页数
     * @param sortProp
     * @param sortOrder
     * @param id
     * @param sd
     * @param ed
     * @param uid
     * @param state
     * @return
     */
    public PageResult queryUserPublish(Integer pagesize, Integer page, String sortProp, String sortOrder, String id, String sd, String ed, String uid, String state) {
//        //调用方法查自己动态总条数
        Long count = quanZiApi.queryUserPublishCount(uid,state,id);
//        分页查询自己动态，查询到动态了之后
        List<Publish> publishList = quanZiApi.queryUserPublish(pagesize, page, sortProp, sortOrder,id,sd,ed, uid,state);
        //创建返回结果对象
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        pageResult.setCounts(Integer.valueOf(count.toString()));
        //计算总页数
        Integer pages = 0;
        if (count % 10 == 0) {
            pages = Integer.valueOf(count.toString()) / 10;
        } else {
            pages = Integer.valueOf(count.toString()) / 10 + 1;
        }
//        =======
//        置顶的话就直接放在集合的最上面
        for (int i = 0; i < publishList.size(); i++) {
            if (publishList.get(i).getTopState() != null) {
                if ("2".equals(publishList.get(i).getTopState().toString())) {
                    Publish publish = publishList.get(i);
                    publishList.remove(i);
                    publishList.add(0, publish);
                }
            }
        }

//        pageResult.setPages(pages);
        //收集用户id
        Set<Long> userIds = publishList.stream().map(Publish::getUserId).collect(Collectors.toSet());

        List<Totals> totalsList = new ArrayList<>();


        Totals totals2 = new Totals();
        totals2.setTitle(StateEnum.DAI_SHEN_HE.getTitle());
        totals2.setCode(StateEnum.DAI_SHEN_HE.getCode());
        totals2.setValue(Integer.valueOf(quanZiApi.queryStateWait(id,Convert.toLong(sd),Convert.toLong(ed)).toString()));

        Totals totals3 = new Totals();
        totals3.setTitle(StateEnum.PASS.getTitle());
        totals3.setCode(StateEnum.PASS.getCode());
        totals3.setValue(Integer.valueOf(quanZiApi.queryStatePass(id,Convert.toLong(sd),Convert.toLong(ed)).toString()));

        Totals totals4 = new Totals();
        totals4.setTitle(StateEnum.NO_PASS.getTitle());
        totals4.setCode(StateEnum.NO_PASS.getCode());
        totals4.setValue(Integer.valueOf(quanZiApi.queryStateNoPass(id,Convert.toLong(sd),Convert.toLong(ed)).toString()));

        Totals totals1 = new Totals();
        totals1.setTitle(StateEnum.ALL.getTitle());
        totals1.setCode(StateEnum.ALL.getCode());
        totals1.setValue(totals2.getValue()+totals3.getValue()+totals4.getValue());


        totalsList.add(totals1);
        totalsList.add(totals2);
        totalsList.add(totals3);
        totalsList.add(totals4);

//        pageResult.setCounts(totals1.getValue());
        pageResult.setTotals(totalsList);
        /*//计算总页数
        Integer pages = 0;
        if (count % 10 == 0) {
            pages = Integer.valueOf(count.toString()) / 10;
        } else {
            pages = Integer.valueOf(count.toString()) / 10 + 1;
        }*/
        if (userIds.size() == 0) {
            return pageResult;
        }
        //根据id查询用户信息
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserInfo::getUserId, userIds);
        List<UserInfo> userInfoList = userInfoMapper.selectList(wrapper);

        //填充数据
        List<Movements> movementsList = new ArrayList<>();
        for (Publish publish : publishList) {
            Movements movements = new Movements();
            movements.setId(publish.getId().toHexString());
            movements.setUserId(Integer.valueOf(publish.getUserId().toString()));
            movements.setText(publish.getText());
            if (publish.getState() != null) {
                movements.setState(publish.getState());
            }
            movements.setCreateDate(publish.getCreated());
            movements.setCommentCount(Integer.valueOf(quanZiApi.queryCommentCount(Convert.toStr(publish.getId())).toString()));
            movements.setLikeCount(Integer.valueOf(quanZiApi.queryLikeCount(Convert.toStr(publish.getId())).toString()));
//            movements.setLikeCount(Integer.valueOf(quanZiApi.queryLikeCount(Convert.toStr(publish.getId())).toString()));
            movements.setReportCount(30); //todo 举报数
            movements.setForwardingCount(30); //todo 转发数
            movements.setMedias(publish.getMedias().toArray(new String[]{}));
            if (publish.getTopState() != null) {
                movements.setTopState(publish.getTopState());
            }
            for (UserInfo userInfo : userInfoList) {
                if (Long.valueOf(movements.getUserId()).longValue() == userInfo.getUserId()) {
                    movements.setUserLogo(userInfo.getLogo());
                    movements.setNickname(userInfo.getNickName());
                    movementsList.add(movements);
                    break;
                }
            }
        }
        pageResult.setItems(movementsList);
        return pageResult;

    }

    /**
     * 根据动态id查询详情
     * @return
     */
    public Movements queryPublishById(String publishId) {
        Publish publish = quanZiApi.queryPublishById(publishId);
        if (publish==null){
//            没有这个id的时候则返回null
            return null;
        }
//        通过LambdaQueryWrapper条件来查询userInfo的信息
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
//        查询用户信息的userid和动态表中的用户id相同的情况开始查询
        wrapper.eq(UserInfo::getUserId,publish.getUserId());
//        查出对应的userid的信息
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);
//          开始填充数据,往Movements中添加数据
        Movements movements = new Movements();
//        十六位加密
        movements.setId(publish.getId().toHexString());
        movements.setUserId(Integer.valueOf(publish.getUserId().toString()));//需要回头看
        movements.setText(publish.getText());
//        状态为null的时候就是也不添加状态，默认为null；
        if (publish.getState() != null) {
            movements.setState(publish.getState());
        }
//        创建日期
        movements.setCreateDate(publish.getCreated());
        movements.setMedias(publish.getMedias().toArray(new String[]{}));
        movements.setCommentCount(Integer.valueOf(quanZiApi.queryCommentCount(Convert.toStr(publish.getId())).toString()));
        movements.setLikeCount(Integer.valueOf(quanZiApi.queryLikeCount(Convert.toStr(publish.getId())).toString()));
        movements.setReportCount(30); //todo 举报数
        movements.setForwardingCount(30); //todo 转发数
        movements.setNickname(userInfo.getNickName());
        movements.setUserLogo(userInfo.getLogo());
        if (publish.getTopState() != null) {
            movements.setTopState(publish.getTopState());
        }
        if (publish.getState() != null) {
            movements.setState(publish.getState());
        }
        movements.setState(publish.getState());
        return movements;
    }

    /**
     * 动态置顶
     * @param id
     * @return
     */
    public Boolean top(String id) {
        return quanZiApi.top(id);
    }


    public Boolean unTop(String id) {
        return quanZiApi.unTop(id);
    }



    /**
     * 通过
     * @param ids
     * @return
     */
    public Boolean messagePass(String[] ids) {
        return quanZiApi.messagePass(ids);
    }

    /**
     * 拒绝
     * @param ids
     * @return
     */
    public Boolean messageReject(String[] ids) {
        return quanZiApi.messageReject(ids);
    }

    /**
     * 撤销
     * @param ids
     * @return
     */
    public Boolean messageRevocation(String[] ids) {
        return quanZiApi.messageRevocation(ids);
    }
}
